package model.vo
{
	/**
	 * @author : LIMIN
	 * @time : 2013-5-16 上午9:52:46
	 * 
	 * @remarks :
	 */
	
	public class MemberNodesVO
	{
		private var _xml:XML;
		public function get xml():XML
		{
			return _xml;
		}
		
		public function MemberNodesVO(arr:Array,nodeCountObj:Object)
		{
			_xml=<obj/>;
			
			/*
			步骤:
			1.转换成XML的Object
			2.遍历建立层次关系
			*/
			var xmlData:Object = generateXmlData(arr, nodeCountObj);
			generateXml(xmlData);
		}
		
		private function generateXmlData(arr:Array, nodeCountObj:Object):Object
		{
			var rtn:Object = {};
			var o:Object, item:XML;
			for each(o in arr)
			{
				item = <new/>;
				addAttr(item, "node_id", o.node_id);
				addAttr(item, "parent_id", o.parent_id);
				addAttr(item, "node_name", o.node_name);
				addAttr(item, "parent_node_str", o.parent_node_str);
				addAttr(item, "sort_id", o.sort_id);
				addAttr(item, "node_count", nodeCountObj[o.node_id] ? nodeCountObj[o.node_id] : "");
				if(int(item.@node_count)>0)
				{
					addAttr(item, "label_str", item.@node_name+"("+item.@node_count+")");
				}else
				{
					addAttr(item, "label_str", item.@node_name);
				}
				rtn[o.node_id] = item;
			}
			return rtn;
		}
		
		private function generateXml(xmlData:Object):void
		{
			var s:String, item:XML;
			for(s in xmlData)
			{
				item = xmlData[s];
				if(item.@parent_id=="0")
				{
					appendChild(_xml, item);
				}else
				{
					appendChild(xmlData[item.@parent_id], item);
				}
			}
		}
		
		private function addAttr(item:XML, name:String, value:String):void
		{
			item.attribute(name)[0] = value;
		}
		
		private function appendChild(item:XML, child:XML):void
		{
			item.appendChild(child);
		}
		
		public function addProperty(propertyName:String, propertyValue:String,isChild:Boolean=false):XML {
			var xmlProperty:XML=<new/>;
			try
			{
				xmlProperty.setName(propertyName);
			}catch(e:Error)
			{
				xmlProperty.setName("key");
				xmlProperty.@value = propertyName;
			}
			
			if(isChild)
			{
				xmlProperty.@value = propertyName;
				xmlProperty.appendChild(new XML(propertyValue).children());
			}else
			{
				xmlProperty.@value = "[" + propertyName + "]" + propertyValue;
				
			}
			_xml.appendChild(xmlProperty);
			
			return xmlProperty;		
		}
	}
}